package com.zpark.resume.controller;

import com.zpark.resume.common.ResponseResult;
import com.zpark.resume.common.StatusCode;
import com.zpark.resume.entity.MyCookie;
import com.zpark.resume.entity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @PackageName: com.zpark.resume.controller
 * @ClassName: RequestsTestController
 * @Description:
 * @author: RZS
 * @date: 2023/7/17  15:32
 */
@RestController
@RequestMapping("/rtc")
public class RequestsTestController {

    // 用户登录表单提交数据
    @PostMapping("/form_login")
    public ResponseResult<User> userLogin(User user){
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), user);
    }

    // 用户登录json格式提交数据
    @PostMapping("/json_login")
    public ResponseResult<User> userLogin002(@RequestBody User user){
        System.out.println(user);
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), user);
    }

    // 获取cookie信息和设置cookie信息
    @GetMapping("/cookie")
    public ResponseResult<List<MyCookie>> getAndSetCookie(HttpServletRequest request, HttpServletResponse response){
        // 获取cookie对象
        Cookie[] cookies = request.getCookies();

        List<MyCookie> lists = new ArrayList<>();

        // 获取请求信息当中的cookie数据
        for (Cookie cookie : cookies) {
            // 获取cookie的key
            String key = cookie.getName();
            // 获取cookie的value
            String value = cookie.getValue();

            MyCookie myCookie = new MyCookie(key, value);

            // 将数据存放到list当中
            lists.add(myCookie);
        }

        // 新建cookie对象
        Cookie cookie = new Cookie("DC", "大锤");
        // 设置cookie信息
        response.addCookie(cookie);

        // 返回请求当中的cookie数据
        return new ResponseResult<>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), lists);
    }

}
